<?php

namespace app\admin\model;

use think\Model;

/**
 * 提成结算
 * @package app\admin\model
 */
class CommissionSettle extends Model
{
    /**
     * 部门财务
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getCommissionSettle($where, $page, $pagesize)
    {
        $data = self::alias('s')
            ->join('user u', 'u.manager = s.sid', 'left')
            ->join('role r', 'r.id = u.role', 'left')
            ->join('resource_official_staff o', 's.sid = o.id', 'left')
            ->field('s.*,r.name as role_name,o.commission_ratio,o.desc as type_name,o.uid,o.alias as username')
            ->where($where)
            ->page($page, $pagesize)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 统计部门财务
     * @param $where
     * @return mixed
     */
    public static function countCommissionSettle($where)
    {
        return self::alias('s')
            ->join('user u', 'u.manager = s.sid', 'left')
            ->join('role r', 'r.id = u.role', 'left')
            ->join('resource_official_staff o', 's.sid = o.id', 'left')
            ->where($where)
            ->count();
    }

    /**
     * 单部门财务
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findCommissionSettle($where)
    {
        $info = self::where($where)->find();

        return $info ? $info->toArray() : [];
    }

    /**
     * 更新部门财务
     * @param $data
     * @param $where
     * @return CommissionSettle
     */
    public static function updateCommissionSettle($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 计算财务
     * @param $where
     * @param $field
     * @return float
     */
    public static function sumCommissionSettle($where, $field)
    {
        return self::where($where)->sum($field);
    }


}